﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewBag.Title = "添加日期";
    Daily daily = ViewBag.daily;
    List<Question> list = ViewBag.list;
}
<style>
    div[name=question] {
        margin: 1em;
        padding: 1em;
        border: 3px dashed #eee;
    }
</style>

<a href="@Url.Action("DailyList")" class="layui-btn">返回日期列表</a>

<div id="daily" class="layui-form">
    <div class="layui-form-item">
        <input name="ID" type="hidden" value="@daily.ID" />
        <label class="layui-form-label">日期</label>
        <div class="layui-input-block">
            <input id="txtrq" name="Date" type="text" value="@daily.Date" class="layui-input" />
        </div>
    </div>
    @*<div class="layui-form-item">
            <div class="layui-input-block">
                <button onclick="Daily_Save()" class="layui-btn">保存日期</button>
            </div>
        </div>*@
</div>
<div class="layui-form layui-form-pane">
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button onclick="addquesttemplate();" class="layui-btn">添加题目模板</button>
        </div>
    </div>
    <div id="questions">
        @foreach (var quest in list)
        {
            <div name="question">
                <div class="layui-form-item">
                    <label class="layui-form-label">题目</label>
                    <div class="layui-input-block">
                        <input name="ID" type="hidden" value="@quest.ID" class="layui-input" />
                        <input name="Title" type="text" value="@quest.Title" class="layui-input" />
                    </div>
                </div>
                @*<div class="layui-form-item">
                        <div class="layui-input-block">
                            <button onclick="addoption(this);" class="layui-btn">添加选项模板</button>
                        </div>
                    </div>*@
                @foreach (var option in quest.Options)
                {
                    <div class="layui-form-item">
                        <div name="option" class="layui-inline">
                            <input name="ID" type="hidden" value="@option.ID" />
                            <label class="layui-form-label">正确答案</label>
                            <div class="layui-input-inline">
                                <input name="IsAnswer" type="checkbox" lay-skin="switch" lay-text="正确|错误" lay-filter="switchia" value="@option.IsAnswer" @(option.IsAnswer ? "checked='checked'" : "") />
                            </div>
                            <label class="layui-form-label">顺序</label>
                            <div class="layui-input-inline">
                                <input name="Order" type="number" value="@option.Order" class="layui-input" />
                            </div>
                            <label class="layui-form-label">内容</label>
                            <div class="layui-input-inline">
                                <input name="Txt" type="text" placeholder="选项" value="@option.Txt" class="layui-input" />
                            </div>
                        </div>
                    </div>

                }
                <div class="layui-form-item">
                    <label class="layui-form-label">解析</label>
                    <div class="layui-input-block">
                        <input name="Analysis" type="text" value="@quest.Analysis" class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">知识点</label>
                    <div class="layui-input-block">
                        <input name="Mark" type="text" value="@quest.Mark" class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button onclick="Question_Delete(this)" class="layui-btn layui-btn-danger">删除本题</button>
                    </div>
                </div>
            </div>
        }
    </div>
    <div id="all">
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button onclick="All_Save()" class="layui-btn layui-btn-normal">保存</button>
            </div>
        </div>
    </div>
</div>
<div id="questtemplate" style="display:none;">

    <div name="question">
        <div class="layui-form-item">
            <label class="layui-form-label">题目</label>
            <div class="layui-input-block">
                <input name="ID" type="hidden" value="0" />
                <input name="Title" type="text" value="" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <div name="option" class="layui-inline">
                <input name="ID" type="hidden" value="0" />
                <label class="layui-form-label">设为答案</label>
                <div class="layui-input-inline">
                    <input name="IsAnswer" type="checkbox" title="是否答案" lay-skin="switch" lay-text="正确|错误" lay-filter="switchia" />
                </div>
                <label class="layui-form-label">顺序</label>
                <div class="layui-input-inline">
                    <input name="Order" type="number" value="1" class="layui-input" />
                </div>
                <label class="layui-form-label">内容</label>
                <div class="layui-input-inline">
                    <input name="Txt" type="text" placeholder="选项A" value="" class="layui-input" />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div name="option" class="layui-inline">
                <input name="ID" type="hidden" value="0" />
                <label class="layui-form-label">设为答案</label>
                <div class="layui-input-inline">
                    <input name="IsAnswer" type="checkbox" title="是否答案" lay-skin="switch" lay-text="正确|错误" lay-filter="switchia" />
                </div>
                <label class="layui-form-label">顺序</label>
                <div class="layui-input-inline">
                    <input name="Order" type="number" value="2" class="layui-input" />
                </div>
                <label class="layui-form-label">内容</label>
                <div class="layui-input-inline">
                    <input name="Txt" type="text" placeholder="选项B" value="" class="layui-input" />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div name="option" class="layui-inline">
                <input name="ID" type="hidden" value="0" />
                <label class="layui-form-label">设为答案</label>
                <div class="layui-input-inline">
                    <input name="IsAnswer" type="checkbox" title="是否答案" lay-skin="switch" lay-text="正确|错误" lay-filter="switchia" />
                </div>
                <label class="layui-form-label">顺序</label>
                <div class="layui-input-inline">
                    <input name="Order" type="number" value="3" class="layui-input" />
                </div>
                <label class="layui-form-label">内容</label>
                <div class="layui-input-inline">
                    <input name="Txt" type="text" placeholder="选项C" value="" class="layui-input" />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div name="option" class="layui-inline">
                <input name="ID" type="hidden" value="0" />
                <label class="layui-form-label">设为答案</label>
                <div class="layui-input-inline">
                    <input name="IsAnswer" type="checkbox" title="是否答案" lay-skin="switch" lay-text="正确|错误" lay-filter="switchia" />
                </div>
                <label class="layui-form-label">顺序</label>
                <div class="layui-input-inline">
                    <input name="Order" type="number" value="4" class="layui-input" />
                </div>
                <label class="layui-form-label">内容</label>
                <div class="layui-input-inline">
                    <input name="Txt" type="text" placeholder="选项D" value="" class="layui-input" />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">解析</label>
            <div class="layui-input-block">
                <input name="Analysis" type="text" placeholder="解析" value="" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">知识点</label>
            <div class="layui-input-block">
                <input name="Mark" type="text" placeholder="知识点" value="" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <button onclick="Question_Delete(this)" class="layui-btn layui-btn-danger">删除本题</button>
            </div>
        </div>
    </div>
</div>
<div id="optiontemplate" style="display:none;">
    <div name="option" class="layui-inline">
        <input name="ID" type="hidden" value="0" />
        <label class="layui-form-label">正确答案</label>
        <div class="layui-input-inline">
            <input name="IsAnswer" type="checkbox" />
        </div>
        <label class="layui-form-label">顺序</label>
        <div class="layui-input-inline">
            <input name="Order" type="number" value="99" />
        </div>
        <label class="layui-form-label">内容</label>
        <div class="layui-input-inline">
            <input name="Txt" type="text" value="" />
        </div>
        <div class="layui-input-inline">
            <button onclick="Option_Save(this)">保存选项</button>
        </div>
    </div>
</div>
@section script{
    <script>
        //日期初始化
        layui.laydate.render({
            elem: '#txtrq'
        });
        layui.form.on('switch(switchia)', function (data) {
            if (data.elem.checked) {
                let answer = $(data.elem);
               let checkboxs = answer.parents("[name=question]").find("[name=option] input:checkbox").each(function (i, e) {
                    $(e).prop("checked", false);
                });
                answer.prop("checked", true);
                layui.form.render("checkbox");
            }
        });
        function addquesttemplate() {
            $("#questions").append($("#questtemplate").html());
            layui.form.render("checkbox");
        }

        function All_Save() {
            let url = "@Url.Action("All_Save")";
            let obj = new Object();
            obj.daily = new Object();
            obj.daily.ID = $("#daily [name=ID]").val();
            obj.daily.Date = $("#daily [name=Date]").val();
            obj.questions = new Array();
            $("#questions [name=question]").each(function (i, ele) {
                let question = new Object();
                question.ID = $(ele).find("input[name=ID]").val();
                question.Title = $(ele).find("input[name=Title]").val();
                question.Analysis = $(ele).find("input[name=Analysis]").val();
                question.Mark = $(ele).find("input[name=Mark]").val();
                let options = new Array();
                $(ele).find("[name=option]").each(function (j, op) {
                    let option = new Object();
                    option.ID = $(op).find("input[name=ID]").val();
                    option.IsAnswer = $(op).find("input[name=IsAnswer]").prop("checked");
                    option.Order = $(op).find("input[name=Order]").val();
                    option.Txt = $(op).find("input[name=Txt]").val();
                    options.push(option);
                });
                question.Options = options;
                obj.questions.push(question);
            });
            layer.msg("正在保存，请稍候...", {
                icon: 16,
                time: 0,
                shade: 0.3,
                shadeClose: false
            });
            $.ajax({
                type: "POST",
                url: url,
                data: obj,
                success: function (result) {
                    if (!result.success) {
                        layer.msg(result.msg);
                    }
                    layer.msg("保存成功，正在刷新页面...", {
                        icon: 1,
                        time: 2000,
                        shade: 0.3,
                        shadeClose: false
                    }, function () {
                            window.location.href = "@Url.Action("DailyList","Admin")";
                    });
                }
            });
        }

        function setAnswer(ele) {
            alert(ele);
            let answer = $(ele);
            if (answer.prop("checked")) {
                answer.parent("[name=question]").find("[name=option]").each(function (i, e) {
                    $(e).prop("checked", false);
                });
                answer.prop("checked", true);
            }
        }
    </script>
}
